package com.tdk.bts.view.manager;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;

import com.tdk.bts.action.FilmScreenAction;
import com.tdk.bts.action.TheaterAction;
import com.tdk.bts.entity.Theater;
import com.tdk.bts.util.ImageUtil;
import com.tdk.bts.util.Request;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class FilmScreenAddView extends JFrame{
	
	private FilmScreenManagerView view ;
	private JTextField nameField;
	private JTextField dayField;
	private JTextField timeField;
	private JLabel time;
	private JComboBox theaterBox;
	private TheaterAction theaterAction =  new TheaterAction(); 
	
	private FilmScreenAction filmScreenAction = new FilmScreenAction();

	public FilmScreenAddView(final FilmScreenManagerView view,final String filmId,String filmName) {
		super(filmName + "场次添加");
		this.view = view;
		setSize(400, 600);
		setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		
		JLabel nameLabel = new JLabel("场次名称");
		nameLabel.setBounds(41, 79, 54, 15);
		getContentPane().add(nameLabel);
		
		nameField = new JTextField();
		nameField.setBounds(124, 76, 205, 21);
		getContentPane().add(nameField);
		nameField.setColumns(10);
		
		JLabel startDayLabel = new JLabel("场次日期");
		startDayLabel.setBounds(41, 148, 54, 15);
		getContentPane().add(startDayLabel);
		
		dayField = new JTextField();
		dayField.setBounds(124, 145, 205, 21);
		getContentPane().add(dayField);
		dayField.setColumns(10);
		
		JLabel dayTip = new JLabel("请使用yyyy-MM-dd格式");
		dayTip.setForeground(Color.RED);
		dayTip.setBounds(124, 176, 205, 15);
		getContentPane().add(dayTip);
		
		time = new JLabel("场次时间");
		time.setBounds(41, 233, 54, 15);
		getContentPane().add(time);
		
		timeField = new JTextField();
		timeField.setBounds(124, 230, 205, 21);
		getContentPane().add(timeField);
		timeField.setColumns(10);
		
		JLabel timeTip = new JLabel("请使用HH:mm:ss格式");
		timeTip.setForeground(Color.RED);
		timeTip.setBounds(124, 255, 186, 15);
		getContentPane().add(timeTip);
		
		JLabel theaterLabel = new JLabel("放映室");
		theaterLabel.setBounds(41, 316, 54, 15);
		getContentPane().add(theaterLabel);
		
		theaterBox = new JComboBox();
		theaterBox.setBounds(124, 310, 205, 21);
		getContentPane().add(theaterBox);
		
	        timeField.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				Request request = new Request();
				request.setParameter("day", dayField.getText());
				request.setParameter("time", timeField.getText());
				try {
					theaterAction.findAll(request);
					List<Theater> ts = (List<Theater>) request.getAttribute("data");
					theaterBox.removeAllItems();
					for (int i = 0; i < ts.size(); i++) {
						//因为Theater的toString为name
						theaterBox.addItem(ts.get(i));
					}
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
			}
			
			@Override
			public void focusGained(FocusEvent e) {}
		});
		
		JLabel addBtn = new JLabel(ImageUtil.getImage("addBtn.png"));
		addBtn.setBounds(140, 375, 119, 39);
		getContentPane().add(addBtn);
		
		addBtn.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					Request request = new Request();
					request.setParameter("name", nameField.getText());
					request.setParameter("startDay", dayField.getText());
					request.setParameter("time", timeField.getText());
					request.setParameter("theaterId", ((Theater)theaterBox.getSelectedItem()).getId());
					request.setParameter("filmId", filmId);
					filmScreenAction.addScreen(request);
					view.bindData(1);
					JOptionPane.showMessageDialog(null, "添加成功", "成功", JOptionPane.ERROR_MESSAGE);
					FilmScreenAddView.this.dispose();
				} catch (Exception e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		setVisible(true);
	}
}
